package com.tan.kotlintest.trace

import android.view.View
import com.tan.kotlintest.R
import com.tan.kotlintest.base.BaseActivity
import com.tan.kotlintest.util.LogUtil
import kotlinx.android.synthetic.main.activity_test_event_pass_layout.*

/*
 * Copyright (C) 2018 重庆一聊高科技有限公司
 * 版权所有
 *
 * 功能描述：测试android TraceView检查方法耗时
 * 作者：txj
 * 创建时间：2019/1/24
 *
 * 修改人：
 * 修改描述：
 * 修改日期
 */
class TestTraceViewActivity : BaseActivity() {

    override fun getContentView(): View? {
        return inflate(R.layout.activity_test_trace_view_layout)
    }

    override fun onContentAdded() {
        setTitleText("测试android TraceView检查方法耗时")
        test_tv.setOnClickListener {
            LogUtil.i("点击了")
            LogUtil.i("数字: " + computeFibonacci(40))
        }
    }

    //代码优化前
    private fun computeFibonacci(positionInFibSequence: Int): Int {
        //0 1 1 2 3 5 8
        return if (positionInFibSequence <= 2) {
            1
        } else {
            computeFibonacci(positionInFibSequence - 1)
            +computeFibonacci(positionInFibSequence - 2)
        }
    }

    //代码优化后
    //将递归换为for循环 同时使用缓存 先将结果缓存起来
    /*private fun computeFibonacci(positionInFibSequence: Int): Int {
        var prev = 0
        var current = 1
        var values: Int
        for (i in 0 until positionInFibSequence) {
            values = prev + current
            prev = current
            current = values
        }
        return current
    }*/

}